package com.game.server.db.dao;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.util.GetBeanFromResultUtil;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class XyNotificationLogDao {

    public static void insertInto(XyNotificationLog info) {
        String sql = "INSERT INTO xy_notification_log (cid,uid,notification_type,msg_type,`state`,createtime,additional_data) VALUES (?,?,?,?,?,NOW(),?)";
        Parameter p1 = new Parameter(Types.INTEGER, info.getCid(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, info.getUid(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, info.getNotification_type(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, info.getMsg_type(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, info.getState(), ParameterType.Input);
        Parameter p6 = new Parameter(Types.VARCHAR, info.getAdditional_data(), ParameterType.Input);
        SqlHelper.execute(sql,p1,p2,p3,p4,p5,p6);
    }

    public static int findByUidApplyCount(int uid, int cid,int notification_type, int msg_type) {
        int count = 0;
        String sql = "select count(id) from xy_notification_log where cid = ? and uid = ? and notification_type=? and msg_type=? and `state`=0";
        Parameter p1 = new Parameter(Types.INTEGER, cid, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, notification_type, ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, msg_type, ParameterType.Input);
        Object scalar = SqlHelper.execScalar(sql, p1,p2,p3,p4);
        count = scalar != null ? (int) Double.parseDouble(scalar.toString()) : 0;
        return count;
    }

    public static List<XyNotificationLog> findByMy(int userId) {
        List<XyNotificationLog> xyNotificationLogs = new ArrayList<>();
        String sql = "select id,cid,uid,notification_type,msg_type,state from xy_notification_log where (cid in(select cid from xy_club_member_info where uid="+userId+" and isManager in(1,2) and del_type=0) and notification_type = 1 and state = 0) or (cid = "+userId+" and notification_type=3 and state = 0) or ( notification_type=2 and cid in(select id from xy_union_info where creator_clubid in(select id from xy_club_info where creatorid="+userId+")) and state = 0)";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                XyNotificationLog info = GetBeanFromResultUtil.getBean(resultSet, XyNotificationLog.class,null);
                xyNotificationLogs.add(info);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xyNotificationLogs;
    }

    public static int findMyNotifCount(int userId) {
        int count = 0;
        String sql = "select count(id) from xy_notification_log where (cid in(select cid from xy_club_member_info where uid="+userId+" and isManager in(1,2) and del_type=0) and notification_type = 1 and state = 0) or (cid = "+userId+" and notification_type=3 and state = 0) or ( notification_type=2 and cid in(select id from xy_union_info where creator_clubid in(select id from xy_club_info where creatorid="+userId+")) and state = 0)";
        Object scalar = SqlHelper.execScalar(sql);
        count = scalar != null ? (int) Double.parseDouble(scalar.toString()) : 0;
        return count;
    }


    public static int findClubNotifCount(int clubid) {
        int count = 0;
        String sql = "select count(id) from xy_notification_log where ((notification_type = 1 and cid="+clubid+") or (notification_type = 2 and cid in(select id from xy_union_info where creator_clubid="+clubid+"))) and `state` = 0";
        Object scalar = SqlHelper.execScalar(sql);
        count = scalar != null ? (int) Double.parseDouble(scalar.toString()) : 0;
        return count;
    }

    public static List<XyNotificationLog> findByClub(Integer id) {
        List<XyNotificationLog> xyNotificationLogs = new ArrayList<>();
        String sql = "select id,cid,uid,notification_type,msg_type,state,additional_data from xy_notification_log where ((notification_type = 1 and cid="+id+") or (notification_type = 2 and cid in(select id from xy_union_info where creator_clubid="+id+"))) and `state` = 0";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                XyNotificationLog info = GetBeanFromResultUtil.getBean(resultSet, XyNotificationLog.class,null);
                xyNotificationLogs.add(info);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xyNotificationLogs;
    }

    /**
     * 根据通知ID查询
     * @param id
     * @return
     */
    public static XyNotificationLog findById(Integer id) {
        XyNotificationLog xyNotificationLog = new XyNotificationLog();
        String sql = "select id,cid,uid,notification_type,msg_type,state,additional_data from xy_notification_log where id="+id;
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            if (resultSet.next()) {
                xyNotificationLog = GetBeanFromResultUtil.getBean(resultSet, XyNotificationLog.class,null);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xyNotificationLog;
    }

    public static int updateState(XyNotificationLog log) {
        String sql = "update xy_notification_log set `state`=? where id=?";
        Parameter p1 = new Parameter(Types.INTEGER, log.getState(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, log.getId(), ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }
}
